﻿using System;
using System.Collections.Generic;
using System.Text;
using CartoLab.GeometryLib.Geometries;
using System.Drawing;

namespace CartoLab.ShortestPath
{
    public class RoutePlanResult
    {
        private string[] passedNodeIDs = null;

        private double weight;

        public RoutePlanResult(string[] strings, double d)
        {
            this.passedNodeIDs = strings;
            this.weight = d;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string[] GetPassedNodeIDs()
        {
            return passedNodeIDs;
        }

        /// <summary>
        /// The passedNodeIDs to set.
        /// </summary>
        /// <param name="passedNodeIDs"></param>
        public void SetPassedNodeIDs(string[] passedNodeIDs)
        {
            this.passedNodeIDs = passedNodeIDs;
        }


        public double GetWeight()
        {

            return weight;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="weight"></param>
        public void SetWeight(double weight)
        {
            this.weight = weight;
        }
        private LineString routeLineString = null;

        public LineString RouteLineString
        {
            get { return routeLineString; }
        }

        private string _startID = "";

        public string StartID
        {
            get { return _startID; }
        }
        private string _endID = "";

        public string EndID
        {
            get { return _endID; }
        }

        internal void CreateRoute(string originID, string destID, Node[] nodes)
        {
            _startID = originID;
            _endID = destID;
            List<Coordinate> cds = new List<Coordinate>();
            Coordinate cdFirst = GetNodeCoordinateByID(originID,nodes);
            cds.Add(cdFirst);
            foreach (string id in this.passedNodeIDs)
            {
                Coordinate cd = GetNodeCoordinateByID(id, nodes);
                cds.Add(cd);
            }
            Coordinate cdLast = GetNodeCoordinateByID(destID, nodes);
            cds.Add(cdLast);

            this.routeLineString = new LineString(cds.ToArray());
        }

        private Coordinate GetNodeCoordinateByID(string id, Node[] nodes)
        {
            foreach (Node node in nodes)
            {
                if (node.ID == id)
                {
                    return node.Postition;
                }
            }
            return null;
        }

        Pen pen = new Pen(Color.Green, 3);
        public void Render(Graphics g)
        {
            List<PointF> pts = new List<PointF>();
            foreach (Coordinate cd in this.RouteLineString.Coordinates)
            {
                PointF pt = ConverCoordinateToPointF(cd);
                pts.Add(pt);
            }
            g.DrawLines(pen, pts.ToArray());
        }

        private PointF ConverCoordinateToPointF(Coordinate cd)
        {
            return new PointF((float)cd.X, (float)cd.Y);
        }
    }
}
